package com.theo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.theo.entity.*;
import com.theo.mapper.HospitalInfoMapper;
import com.theo.service.*;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Controller
@RequestMapping("/hospital-info")
public class HospitalInfoController {
    @Autowired
    private HospitalInfoService hospitalInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private StockInfoService stockInfoService;
    @Autowired
    private MaskInfoService maskInfoService;
    @Autowired
    private VaccineInfoService vaccineInfoService;

    @RequestMapping("/show.do")
    public String show(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 5);
        List<HospitalInfo> list = hospitalInfoService.queryAllHospitalInfo();
        PageInfo pageInfo = new PageInfo(list, 8);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/hospital_list";
    }
    @RequestMapping("/show")
    public String showH(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 5);
        List<HospitalInfo> list = hospitalInfoService.queryAllHospitalInfo();
        PageInfo pageInfo = new PageInfo(list, 8);
        model.addAttribute("pageInfo", pageInfo);
        return "user/hospital_list";
    }

    @RequestMapping("/delete.do")
    public String delete(Integer id) {
        hospitalInfoService.deleteHospitalInfo(id);
        return "redirect:/hospital-info/show.do";
    }

    @RequestMapping("/toMaskStock.do")
    public String toMaskStock(Integer id,Model model) {
        List<StockInfo> stockInfos = stockInfoService.queryStockInfoByHospitalId(id);
        model.addAttribute("stockInfos", stockInfos);
        List<MaskInfo> maskInfos = maskInfoService.queryAllMaskInfo();
        model.addAttribute("maskInfos", maskInfos);
        return "user/stock_mask";
    }

    @RequestMapping("/toVaccineStock.do")
    public String toVaccineStock(Integer id,Model model) {
        List<StockInfo> stockInfos = stockInfoService.queryStockInfoByHospitalId(id);
        model.addAttribute("stockInfos", stockInfos);
        List<VaccineInfo> vaccineInfos = vaccineInfoService.queryAllVaccineInfo();
        model.addAttribute("vaccineInfos", vaccineInfos);
        return "user/stock_vaccine";
    }

    @RequestMapping("/update.do")
    public String update(HospitalInfo hospitalInfo) {
        hospitalInfoService.updateHospitalInfo(hospitalInfo);
        return "redirect:/hospital-info/show.do";
    }

    @RequestMapping("/showLimit")
    public String showLimit(@RequestParam("cId") String cId, @RequestParam("pId") String pId, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 5);
        Map<String, Object> map = new HashMap<>();
        map.put("cId", cId);
        map.put("pId", pId);
        System.out.println(map.get("pId"));
        System.out.println(map.get("cId"));
        List<HospitalInfo> list = hospitalInfoService.queryHospitalInfoByCityId(map);
        list.forEach(System.out::println);
        PageInfo pageInfo = new PageInfo(list, 8);
        model.addAttribute("pageInfo", pageInfo);
        return "user/hospital_list";
    }
}

